//
//  ServiceContant.swift
//  APIHeartRate
//
//  Created by Tek on 2023/1/6.
//

import Foundation
import CoreBluetooth

struct HRServiceConst {
    static let cmdVibrate: UInt8 = 0x01
    static let cmdSyncTime: UInt8 = 0x02
    static let cmdSerial: UInt8 = 0x03
    static let cmdStep: UInt8 = 0x04
    static let cmdOxygen: UInt8 = 0x05
    static let cmdPlay: UInt8 = 0x06
    static let cmdReset: UInt8 = 0x07
    static let cmdUnbind: UInt8 = 0x08
    static let cmdBattery: UInt8 = 0x0a
    static let cmdMeasure: UInt8 = 0x0b
    static let cmdMac: UInt8 = 0x0c
    static let cmdSetMode: UInt8 = 0xd
    static let cmdMode: UInt8 = 0x0e
    static let cmdPressure: UInt8 = 0x0f
    static let cmdCurrent: UInt8 = 0x10
    static let cmdKeepLive: UInt8 = 0x11
    static let cmdModeDetail: UInt8 = 0x13
    static let cmdAlgoOut: UInt8 = 0x18
    
    static let cmdDataDay: UInt8 = 0x20
    static let cmdDataDayStart: UInt8 = 0x21
    static let cmdDataDayContent: UInt8 = 0x22
    static let cmdDataDayCheck: UInt8 = 0x23
    
    static let cmdDataDaily: UInt8 = 0x24
    static let cmdDataDailyStart: UInt8 = 0x25
    static let cmdDataDailyContent: UInt8 = 0x26
    static let cmdDataDailyCheck: UInt8 = 0x27
    
    static let cmdDataSport: UInt8 = 0x28
    static let cmdDataSportStart: UInt8 = 0x29
    static let cmdDataSportContent: UInt8 = 0x2a
    static let cmdDataSportCheck: UInt8 = 0x2b
    
    static let cmdDataSleep: UInt8 = 0x2c
    static let cmdDataSleepStart: UInt8 = 0x2d
    static let cmdDataSleepContent: UInt8 = 0x2e
    static let cmdDataSleepCheck: UInt8 = 0x2f
    
    static let cmdDataExcercise: UInt8 = 0x30
    static let cmdDataExcerciseStart: UInt8 = 0x31
    static let cmdDataExcerciseContent: UInt8 = 0x32
    static let cmdDataExcerciseCheck: UInt8 = 0x33
    
    static let queryHeartRateMode: UInt8 = 0x43
    
    static let cmdDaliyAck: UInt8 = 0x34
    static let cmdSportAck: UInt8 = 0x35
    
    static let cmdDoNotTurb: UInt8 = 0x14
    static let cmdQueryInfo: UInt8 = 0x46
    
    
    static func cmdLight(on: Bool) -> Data {
        return Data([0x40, on ? 0x01 : 0x00])
    }
    
    static func cmdDynamicHeartRate(on: Bool) -> Data {
        return Data([0x41, on ? 0x01 : 0x00])
    }
    
    static func cmdGymnastic(on: Bool) -> Data {
        return Data([0x44, on ? 0x01 : 0x00])
    }
    
    static func mockRate(on: Bool) -> Data {
        return Data([0x47, on ? 0x01 : 0x00])
    }
    
    static func scenarioCmd(type: ScenarioType) -> Data {
        switch(type) {
        case .lesson:
            return Data([0x49,0x00])
        case .quickWalk:
            return Data([0x49,0x01])
        case .daily:
            return Data([0x49,0x02])
        }
    }
    
    // MARK: Properties
    static let batteryServiveID = CBUUID(string: "180F")
    static let batteryCharacteristic = CBUUID(string: "2A19")
    
    static let macServiveID = CBUUID(string: "FEE7")
    static let macCharacteristic = CBUUID(string: "FEC9")
    
    static let generalServiveID = CBUUID(string: "180A")
    static let manuCharacteristic = CBUUID(string: "2A29")
    static let modelCharacteristic = CBUUID(string: "2A24")
    static let hardwareCharacteristic = CBUUID(string: "2A27")
    static let firmwareCharacteristic = CBUUID(string: "2A26")
    static let softwareCharacteristic = CBUUID(string: "2A28")
    static let sysIDCharacteristic = CBUUID(string: "2A23")
    
    static let heartRateServiveID = CBUUID(string: "180D")
    static let heartNotifyCharacteristic = CBUUID(string: "2A37")
    static let heartReadCharacteristic = CBUUID(string: "2A38")
    
    static let customServiveID  = CBUUID(string: "66880000-0000-1000-8000-008012563489")
    static let writeCharacteristic  = CBUUID(string: "66880001-0000-1000-8000-008012563489")
    static let notifyCharacteristic  = CBUUID(string: "66880002-0000-1000-8000-008012563489")
    static let Characteristic = CBUUID(string: "2A19")
    /**
     OTA 相关服务
     */
    static let otaUpdateServiceIDUUID = CBUUID(string: "FF00")
    static let otaUpdateCharUUID = CBUUID(string: "FF01")
    
    static func characteristicUUIDsForServiceUUID(_ service: CBUUID) -> [CBUUID]{
        if (service == batteryServiveID) {
            return [batteryCharacteristic]
        } else if (service == generalServiveID) {
            return [manuCharacteristic, modelCharacteristic, hardwareCharacteristic, firmwareCharacteristic, softwareCharacteristic,sysIDCharacteristic]
        } else if (service == heartRateServiveID) {
            return [heartNotifyCharacteristic,heartReadCharacteristic]
        } else if (service == customServiveID) {
            return [notifyCharacteristic,writeCharacteristic]
        } else if (service == otaUpdateServiceIDUUID) {
            return [otaUpdateCharUUID]
        }
        return []
    }
}
